package com.huibao.hulibao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huibao.hulibao.R;
import com.huibao.hulibao.entity.MessageEntity;
import com.huibao.hulibao.fragment.MessageFragmentView;
import com.huibao.hulibao.utils.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class MsgLvAdapter extends BaseAdapter {
    private ArrayList<MessageEntity.MessageDetail> mDatas;
    private Context mContext;
    private MessageFragmentView mfv;


    private LayoutInflater mInflater;

    public MsgLvAdapter(ArrayList<MessageEntity.MessageDetail> datas, Context context, MessageFragmentView mfv) {
        this.mDatas = datas;
        this.mContext = context;
        this.mfv = mfv;

        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {

        return null != mDatas ? mDatas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final MessageEntity.MessageDetail md = (MessageEntity.MessageDetail) getItem(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_msg, parent, false);
            holder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // holder.mTextView.setText(mDatas.get(position));

        holder.tv_msg_content.setText(md.Msg);

        if (md.Status.equals("-30")) {
            holder.tv_is_see.setText("忙碌");
        } else if (md.Status.equals("-20")) {
            holder.tv_is_see.setText("未分配");
        } else if (md.Status.equals("-10")) {
            holder.tv_is_see.setText("未确认");
            holder.iv_is_see.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.msg_no_see));
            int color = mContext.getResources().getColor(R.color.white);
            holder.tv_msg_confirm.setText("确认");
            holder.tv_msg_confirm.setTextColor(color);
            holder.tv_msg_confirm.setBackground(mContext.getResources().getDrawable(R.drawable.corner_view_qianlan_5radio));
        } else if (md.Status.equals("0")) {
            holder.tv_is_see.setText("已确认");
            holder.iv_is_see.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.msg_see));
            holder.tv_msg_confirm.setText("关闭");
            int color = mContext.getResources().getColor(R.color.text_black);
            holder.tv_msg_confirm.setTextColor(color);
            holder.tv_msg_confirm.setBackground(mContext.getResources().getDrawable(R.drawable.corner_view_close_5radio));
        } else if (md.Status.equals("1")) {
            holder.tv_is_see.setText("关闭");
        }


        holder.tv_msg_time.setText("系统消息    " + md.AddTime);

        holder.tv_msg_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mfv.operMessageData(md.Status, md.Id, true);
            }
        });

        holder.rl_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                T.show(mContext, md.Msg, 1);
            }
        });


        return convertView;
    }


    static class ViewHolder {

        @BindView(R.id.rl_main)
        RelativeLayout rl_main;
        @BindView(R.id.iv_is_see)
        ImageView iv_is_see;
        @BindView(R.id.tv_is_see)
        TextView tv_is_see;
        @BindView(R.id.tv_msg_content)
        TextView tv_msg_content;
        @BindView(R.id.tv_msg_time)
        TextView tv_msg_time;
        @BindView(R.id.tv_msg_confirm)
        TextView tv_msg_confirm;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
